package cn.edu.sgu.www.mhxysy.service.repository.impl;

import cn.edu.sgu.www.mhxysy.consts.IdentifierPrefixes;
import cn.edu.sgu.www.mhxysy.entity.repository.PackageGrid;
import cn.edu.sgu.www.mhxysy.entity.repository.RolePackage;
import cn.edu.sgu.www.mhxysy.mapper.repository.PackageGridMapper;
import cn.edu.sgu.www.mhxysy.mapper.repository.RolePackageMapper;
import cn.edu.sgu.www.mhxysy.redis.RedisUtils;
import cn.edu.sgu.www.mhxysy.service.repository.RolePackageService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class RolePackageServiceImpl implements RolePackageService {

    private final RedisUtils redisUtils;
    private final PackageGridMapper packageGridMapper;
    private final RolePackageMapper rolePackageMapper;

    @Autowired
    public RolePackageServiceImpl(
            RedisUtils redisUtils,
            PackageGridMapper packageGridMapper,
            RolePackageMapper rolePackageMapper) {
        this.redisUtils = redisUtils;
        this.packageGridMapper = packageGridMapper;
        this.rolePackageMapper = rolePackageMapper;
    }

    @Override
    public void init(String roleId) {
        // 查询角色包裹
        Map<String, Object> columnMap = new HashMap<>();

        columnMap.put("role_id", roleId);

        List<RolePackage> list = rolePackageMapper.selectByMap(columnMap);

        if (list.size() > 0) {
            RolePackage rolePackage = new RolePackage();

            if (list.size() > 1) {
                // 删除角色包裹
                rolePackageMapper.deleteByMap(columnMap);

                // 添加角色包裹
                rolePackage.setId(rolePackage.uuid());
                rolePackage.setRoleId(roleId);

                rolePackageMapper.insert(rolePackage);
            } else {
                rolePackage = list.get(0);
            }

            // 查询道具格子
            columnMap.clear();
            columnMap.put("package_id", rolePackage.getId());

            List<PackageGrid> packageGrids = packageGridMapper.selectByMap(columnMap);
            int count = packageGrids.size();

            if (count > 0) {
                // 删除道具格子
                packageGridMapper.deleteByMap(columnMap);

                // 批量添加道具格子（跟原来数量一致）
                for (int i = 1; i <= count; i++) {
                    PackageGrid packageGrid = new PackageGrid();

                    packageGrid.setId(IdentifierPrefixes.PREFIX_BGGZ + redisUtils.uuid());
                    packageGrid.setLocation(i);
                    packageGrid.setPackageId(rolePackage.getId());

                    packageGridMapper.insert(packageGrid);
                }
            }
        }
    }

    @Override
    public void unlock(String roleId) {
        // 查询角色包裹
        QueryWrapper<RolePackage> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("role_id", roleId);

        RolePackage rolePackage = rolePackageMapper.selectOne(queryWrapper);

        if (rolePackage != null) {
            // 查询仓库格子数量
            QueryWrapper<PackageGrid> packageGridQueryWrapper = new QueryWrapper<>();

            packageGridQueryWrapper.eq("package_id", rolePackage.getId());

            Long count = packageGridMapper.selectCount(packageGridQueryWrapper);

            // 解锁5个仓库格子
            for (int location = (int) (count + 1); location < count + 6; location++) {
                PackageGrid packageGrid = new PackageGrid();

                packageGrid.setId(packageGrid.uuid() + location);
                packageGrid.setLocation(location);
                packageGrid.setPackageId(rolePackage.getId());

                packageGridMapper.insert(packageGrid);
            }
        }
    }

}